package org.red5.core;

/*
 * RED5 Open Source Flash Server - http://www.osflash.org/red5
 * 
 * Copyright (c) 2006-2008 by respective authors (see below). All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

import java.util.ArrayList;
import java.util.Arrays;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.ServiceUtils;
import org.red5.server.api.so.ISharedObject;
/**
 * Sample application that uses the client manager.
 * 
 * @author The Red5 Project (red5@osflash.org)
 */
public class Application extends ApplicationAdapter {

	//variables
	private ISharedObject		sharedObject;
	private ArrayList<User> userArray;
	
	
	/** {@inheritDoc} */
    @Override
	public boolean connect(IConnection conn, IScope scope, Object[] params) {
    	addUser(params[0].toString(),params[1].toString());
    	
		return true;
	}

	/** {@inheritDoc} */
    @Override
	public void disconnect(IConnection conn, IScope scope) {
		super.disconnect(conn, scope);
	}
    
    /**
     * Wird aufgerufen, wenn sich der Server startet
     */
    public boolean appStart(IScope scope){
    	userArray = new ArrayList<User>();
    	System.out.println("[INFO] [Launcher:/VorlesungsTool] Nobody Is Perfect! created");
	    return true;
    }
    
    /**
     * Wird aufgerufen, wenn sich der erste User anmeldet. (evtl auch mehrmals)
     */
    @Override
    public boolean roomStart(IScope room) 
    {
    	if (!super.roomStart(room))
    	{
	    	  System.out.println("Fehler beim Starten des Rooms"); 
	    	  return false;  
	    }
    	
    	System.out.println("Start des Raums");
    	
    	createSharedObject(room, "sharedObject", false);
    	sharedObject = getSharedObject(room, "sharedObject");
    	
    	return true;
    }
    
    
    public void addUser(String name, String password)
    {
    	System.out.println("Name: "+name + "Passwort: " +password);
//    	sharedObject.setAttribute("name", name);
    	userArray.add(new User(name, password));
    }
    
    public void startGame(String userName)
    {
    	sharedObject.beginUpdate();
    	
    	sharedObject.setAttribute("startGame", true);
    	sharedObject.setAttribute("gameMaster", "name");
    	
    	sharedObject.endUpdate();
    }
    
    public ArrayList<User> getUserArray()
    {
    	return userArray;
    }
    
}
